package org.jeecg.modules.friends.service.impl;

import java.util.Date;

import org.jeecg.modules.friends.service.UserAccess;
import org.jeecg.modules.friends.store.entity.UserAccessEntity;
import org.jeecg.modules.friends.store.mapper.UserAccessMapper;
import org.jeecg.modules.friends.store.wrapper.UserAccessQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2025-01-10
 */
@Service
@Slf4j
public class UserAccessImpl implements UserAccess {
    private static final long max_times = 10;
    @Autowired
    private UserAccessMapper userAccessMapper;

    @Override
    public boolean isCanAccess(long userId, String targetName) {
        Date curDate = DateUtil.beginOfDay(new Date()).toJdkDate();
        UserAccessEntity userAccessEntity = userAccessMapper.findOne(
                new UserAccessQuery()
                        .where
                        .userId().eq(userId)
                        .targetName().eq(targetName)
                        .end()
        );
        if (userAccessEntity == null || userAccessEntity.getLatelyCount() == null) {
            return true;
        }
        Date latelyTime = userAccessEntity.getLatelyTime();
        if (latelyTime == null || latelyTime.before(curDate)) {
            return true;
        }
        return false;
    }
}
